package com.ms.p2p.template.directive;

import com.alibaba.dubbo.config.annotation.Reference;
import com.google.common.collect.Lists;
import com.ms.common.utils.DirectiveUtils;
import com.ms.p2p.entity.system.Dictionary;
import com.ms.p2p.provider.DictionaryProvider;
import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * Created by xing_ on 2017/5/21.
 */
@Repository
public class DictDirective extends BaseDirective {

    @Reference
    private DictionaryProvider dictionaryProvider;

    /**nid**/
    public static final String NID = "nid";

    @Override
    public void execute(Environment env, Map params, TemplateModel[] templateModels,
                        TemplateDirectiveBody body) throws TemplateException, IOException {
        String type = DirectiveUtils.getString(NID, params);
        if(StringUtils.isNotBlank(type)){
            List<Dictionary> list=  dictionaryProvider.getdictListByTypeId(type);
            renderParamToBody("dictList", list, env, body);
        }
        else{
            renderParamToBody("dictList", Lists.newArrayList(), env, body);
        }

    }
}
